package io.renren.socket;

import com.alibaba.fastjson.JSONObject;
import io.netty.channel.ChannelHandlerContext;
import io.renren.common.utils.Constant;
import io.renren.common.utils.StringUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SendMsgRes {
    private static Logger logger = LoggerFactory.getLogger(SendMsgRes.class);
    public static void sendJsonResMsgOKorERROR(String action, String OKorERROR, String msgcontent, JSONObject obj, String strkey, JSONObject conobj, ChannelHandlerContext ctx){
        if(action == null || action.equals("")) {
            logger.error("sendJsonResMsgOKorERROR1 action=null");
            return;
        }
        try {
            JSONObject objRsp = new JSONObject();
            byte am = obj.getByte("协议类型");
            objRsp.put("从站编码",obj.getString("从站编码"));
            objRsp.put("从站地址",obj.getString("从站地址"));//IP地址
            objRsp.put("协议类型",am);//0x00—104协议，0x01—私有协议

            if(am == Constant.AM_IEC104) {
                objRsp.put("类型标识", Constant.M_SP_NA_1);
                objRsp.put("信息对象地址", Constant.M_SP_NA_1);//只有类型标志为136才是数组
                objRsp.put("遥控类型", Constant.M_SP_NA_1);
                objRsp.put("输出⽅式", Constant.M_SP_NA_1);
                objRsp.put("设置值", (byte) Constant.M_SP_NA_1);//只有类型标志为136才是数组
            } else if(am == Constant.AM_PRIVAT) {
                JSONObject uploadFileRspobj = new JSONObject();
                uploadFileRspobj.put(Constant.CMD_ACTION,action);
                uploadFileRspobj.put(Constant.CMD_MSGID,obj.getString("msgId"));
                uploadFileRspobj.put(Constant.CMD_RESULT,OKorERROR);
                if(OKorERROR.equals(Constant.CMD_OK)) {
                    if(msgcontent != null && !msgcontent.equals("")) {
                        uploadFileRspobj.put(Constant.CMD_MSG, msgcontent);
                    }else{
                        uploadFileRspobj.put(Constant.CMD_MSG,Constant.CMD_SUCCESS);
                    }
                } else {
                    if(msgcontent != null && !msgcontent.equals("")) {
                        uploadFileRspobj.put(Constant.CMD_MSG, msgcontent);
                    }else{
                        uploadFileRspobj.put(Constant.CMD_MSG,Constant.CMD_ERROR);
                    }
                }

                if(strkey != null && conobj != null){
                    uploadFileRspobj.put(strkey,conobj);
                }

                JSONObject privateaminformation = obj.getJSONObject("私有协议消息");
                String sessionid = privateaminformation.getString("会话编号");
                logger.debug("私有协议消息:" + privateaminformation + " 会话编号:" + sessionid);

                JSONObject obj2 = new JSONObject();//只有私有协议才有该字段
                obj2.put("会话编号", sessionid);//会话ID，data区数据所属终端SN号
                obj2.put("消息内容", uploadFileRspobj);//私有协议消息数据，DATA内容
//                        array.add(obj2);
                objRsp.put("私有协议消息", obj2);//只有私有协议才有该字段
            }
            sendJsonResMsg(objRsp,ctx);

        }catch(Exception e){
            logger.error("sendJsonResMsgOKorERROR1 error： " + e);
        }
    }

    private static void sendJsonResMsg(JSONObject obj,ChannelHandlerContext ctx){

        try {
//            String msg = JSONObject.toJSONString(obj);

            String objStr = obj.toJSONString();
            logger.debug("c-应答jsonStr:" + objStr);
//            String escstr = StringEscapeUtils.unescapeJava(objStr);
//            logger.debug("c-应答escstr:" + escstr);
            String Unistr = StringUtils.decodeUnicode(objStr);
            logger.debug("c-应答Unistr:" + Unistr);
            String unstr = StringUtils.convertUnicodeToCh(objStr);
            logger.debug("c-应答unstr:" + unstr);
            String msgstrH = StringUtils.toChineseHex(objStr);
//            String msgstrH = StringUtils.toChineseHex(objStr);
            String jsonSize = String.format("%08X", msgstrH.length() / 2);
            String header = "FFFFFFFF" + jsonSize;
            logger.debug("c-header + msgstrH:" + header + msgstrH);
            ctx.channel().writeAndFlush(header + msgstrH);
        }catch(Exception e){
            logger.error("c-sendJsonResMsg error： " + e);
        }
    }
}
